package com.blog4j.system.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.blog4j.api.client.FeignUser;
import com.blog4j.api.vo.UserInfoVo;
import com.blog4j.common.enums.ApproveEnum;
import com.blog4j.common.enums.ErrorEnum;
import com.blog4j.common.enums.NoticeStatusEnum;
import com.blog4j.common.enums.NoticeTypeEnum;
import com.blog4j.common.exception.Blog4jException;
import com.blog4j.system.entity.NoticeEntity;
import com.blog4j.system.mapper.NoticeMapper;
import com.blog4j.system.service.NoticeService;
import com.blog4j.system.vo.req.CreateNoticeReqVo;
import com.blog4j.system.vo.req.EditNoticeReqVo;
import com.blog4j.system.vo.req.NoticeListReqVo;
import com.blog4j.system.vo.resp.NoticeRespVo;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author 98k灬
 * @version v1.0.0
 * @Description : 功能描述
 * @Create on : 2024/7/20 08:58
 **/
@Service
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, NoticeEntity>
        implements NoticeService {
    private final FeignUser feignUser;

    /**
     * 获取公告信息
     *
     * @param noticeType 公告类型
     * @return 公告信息
     */
    @Override
    public NoticeRespVo systemNotice(Integer noticeType) {
        LambdaQueryWrapper<NoticeEntity> wrapper = new LambdaQueryWrapper<NoticeEntity>()
                .eq(NoticeEntity::getNoticeStatus, NoticeStatusEnum.ONLINE.getCode())
                .eq(NoticeEntity::getNoticeType, NoticeTypeEnum.SYSTEM_NOTICE.getCode())
                .orderByDesc(NoticeEntity::getCreateTime);
        NoticeEntity notice = this.baseMapper.selectOne(wrapper);
        if (Objects.isNull(notice)) {
            return null;
        }

        NoticeRespVo noticeRespVo = new NoticeRespVo();
        BeanUtil.copyProperties(notice, noticeRespVo);
        return noticeRespVo;
    }

    /**
     * 获取全部公告信息
     *
     * @param reqVo 请求信息
     * @return 全部公告信息
     */
    @Override
    public PageInfo<NoticeRespVo> allNotice(NoticeListReqVo reqVo) {
        LambdaQueryWrapper<NoticeEntity> wrapper = new LambdaQueryWrapper<NoticeEntity>()
                .orderByDesc(NoticeEntity::getNoticeStatus);
        Page<Object> page = PageHelper.startPage(reqVo.getPageNo(), reqVo.getPageSize());
        List<NoticeEntity> noticeList = this.baseMapper.selectList(wrapper);
        if (CollectionUtil.isEmpty(noticeList)) {
            return new PageInfo<>(new ArrayList<>());
        }

        List<NoticeRespVo> list = noticeList.stream().map(item -> {
            NoticeRespVo respVo = new NoticeRespVo();
            BeanUtil.copyProperties(item, respVo);
            return respVo;
        }).collect(Collectors.toList());

        PageInfo<NoticeRespVo> pageInfo = new PageInfo<>(list);
        pageInfo.setTotal(page.getTotal());
        return pageInfo;
    }

    /**
     * 获取公告详情
     *
     * @param id 公共ID
     * @return 告详情
     */
    @Override
    public NoticeRespVo noticeDetail(Integer id) {
        NoticeEntity notice = this.baseMapper.selectById(id);
        if (Objects.isNull(notice)) {
            throw new Blog4jException(ErrorEnum.INVALID_PARAMETER_ERROR);
        }
        UserInfoVo userInfoVo = feignUser.getUserInfoByUserId(notice.getCreater());
        if (StringUtils.isBlank(userInfoVo.getUserId())) {
            throw new Blog4jException(ErrorEnum.USER_NOT_EXIST_ERROR);
        }

        NoticeRespVo respVo = new NoticeRespVo();
        BeanUtil.copyProperties(notice, respVo);
        respVo.setCreaterName(userInfoVo.getUserName());
        return respVo;
    }

    /**
     * 编辑公告
     *
     * @param reqVo 请求信息
     */
    @Override
    public void editNotice(EditNoticeReqVo reqVo) {
        Integer id = reqVo.getId();
        NoticeEntity notice = this.baseMapper.selectById(id);
        if (Objects.isNull(notice)) {
            throw new Blog4jException(ErrorEnum.INVALID_PARAMETER_ERROR);
        }

        Integer noticeStatus = reqVo.getNoticeStatus();
        if (Objects.equals(NoticeStatusEnum.ONLINE.getCode(), noticeStatus)) {
            LambdaQueryWrapper<NoticeEntity> wrapper = new LambdaQueryWrapper<NoticeEntity>()
                    .ne(NoticeEntity::getId, id)
                    .eq(NoticeEntity::getNoticeStatus, NoticeStatusEnum.ONLINE.getCode());
            if (this.baseMapper.selectCount(wrapper) > 0) {
                throw new Blog4jException(ErrorEnum.NOTICE_ONLINE_ALREADY_EXIST);
            }
        }

        notice.setNoticeStatus(noticeStatus);
        notice.setNoticeContent(reqVo.getNoticeContent());
        this.baseMapper.updateById(notice);
    }

    /**
     * 删除公告
     *
     * @param id 公告ID
     */
    @Override
    public void delNotice(Integer id) {
        NoticeEntity notice = this.baseMapper.selectById(id);
        if (Objects.isNull(notice)) {
            throw new Blog4jException(ErrorEnum.INVALID_PARAMETER_ERROR);
        }
        this.baseMapper.deleteById(id);
    }

    /**
     * 创建公告
     *
     * @param reqVo 请求信息
     */
    @Override
    public void createNotice(CreateNoticeReqVo reqVo) {
        Integer noticeType = reqVo.getNoticeType();
        if (!Objects.equals(noticeType, NoticeTypeEnum.SYSTEM_NOTICE.getCode()) &&
                !Objects.equals(noticeType, NoticeTypeEnum.ORGANIZATION_NOTICE.getCode())) {
            throw new Blog4jException(ErrorEnum.INVALID_PARAMETER_ERROR);
        }

        NoticeEntity notice = new NoticeEntity();
        BeanUtil.copyProperties(reqVo, notice);

        String userId = StpUtil.getLoginIdAsString();
        notice.setCreater(userId).setNoticeStatus(NoticeStatusEnum.WAIT.getCode());
        this.baseMapper.insert(notice);
    }
}
